package yawning.mew.gui.characters;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpinnerNumberModel;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;

import yawning.mew.character.Clazz;
import yawning.mew.character.Race;
import yawning.mew.gui.characters.attributes.AttributeContainer;
import yawning.mew.gui.characters.glyphs.GlyphsContainer;
import yawning.mew.gui.characters.talents.TalentsPanel;
import yawning.mew.gui.tools.CharacterDataSource;

@SuppressWarnings("serial")
public class ProfileContainer extends JPanel {
	private JComboBox cmbClass;
	private JComboBox cmbRace;
	private JSpinner spnLevel;
	private AttributeContainer attributeContainer;
	private CharacterDataSource toon;
	private TalentsPanel talentsPanel;
	private GlyphsContainer glyphsPanel;

	
	public ProfileContainer(CharacterDataSource toon)
	{
		setLayout(null);
		this.toon = toon;
		
		JTextPane textPane_1 = new JTextPane();
		textPane_1.setText("Race:");
		textPane_1.setBounds(10, 11, 54, 22);
		add(textPane_1);
		
		//TODO: Once other classes can use this application, change to Race.values()
		cmbRace = new JComboBox(new Race[]{Race.NIGHT_ELF,Race.TAUREN,Race.TROLL,Race.WORGEN});
		cmbRace.setSelectedIndex(-1);
		cmbRace.setBounds(74, 11, 106, 22);
		add(cmbRace);
		
		JTextPane textPane_3 = new JTextPane();
		textPane_3.setText("Class:");
		textPane_3.setBounds(190, 11, 54, 22);
		add(textPane_3);
		
		cmbClass = new JComboBox();
		cmbClass.setEnabled(false);
		cmbClass.setEditable(false);
		cmbClass.setBounds(254, 11, 106, 22);
		add(cmbClass);
		
		JTextPane textPane_4 = new JTextPane();
		textPane_4.setText("Level:");
		textPane_4.setBounds(370, 11, 54, 22);
		add(textPane_4);
		
		spnLevel = new JSpinner();
		spnLevel.setModel(new SpinnerNumberModel(0, 0, 85, 1));
		spnLevel.setBounds(434, 13, 54, 20);
		add(spnLevel);
		
		this.toon = toon;
		
		cmbRace.addItemListener(new CmbRace_Listener(cmbClass));
		
		JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
		tabs.setBounds(10, 44, 733, 507);
		add(tabs);
		
		JScrollPane scrAttributes = new JScrollPane();
		scrAttributes.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrAttributes.getVerticalScrollBar().setValue(0);
		scrAttributes.getHorizontalScrollBar().setValue(0);
		
		attributeContainer = new AttributeContainer(toon);
		scrAttributes.setViewportView(attributeContainer);
		tabs.addTab("Attributes", scrAttributes);
		tabs.setEnabledAt(0, true);
		
		JScrollPane scrTalents = new JScrollPane();
		scrTalents.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrTalents.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrTalents.getVerticalScrollBar().setValue(0);
		scrTalents.getHorizontalScrollBar().setValue(0);
		talentsPanel = new TalentsPanel(toon);
		scrTalents.setViewportView(talentsPanel);
		tabs.addTab("Talents", null, scrTalents, null);
		tabs.setEnabledAt(1, true);
		
		glyphsPanel = new GlyphsContainer(toon);
		tabs.addTab("Glyph", null, glyphsPanel, null);
		tabs.setEnabledAt(2, true);
		
		initDataBindings();
	}
	
	/**
	 *	Listener for the race ComboBox,  
	 *
	 */
	private class CmbRace_Listener implements ItemListener
	{
		JComboBox classComboBox;
		
		public CmbRace_Listener(JComboBox classComboBox){
			this.classComboBox = classComboBox;
		}
		@Override
		public void itemStateChanged(ItemEvent arg0) {
			classComboBox.removeAllItems();
			classComboBox.setEnabled(false);
			Object item = arg0.getItem();
			if(item instanceof Race){
				//Currently we only support Druids but this code will be used eventually.
				/*Clazz[] classes = RaceClassRestrictions.getClassFromRace((Race)item);
				
				for (Clazz clazz : classes) {
					classComboBox.addItem(clazz);
				}
				classComboBox.setEnabled(true);
				classComboBox.setSelectedIndex(-1);
				*/
				
				classComboBox.addItem(Clazz.DRUID);
				classComboBox.setSelectedIndex(0);
				//TODO call talents so I can set the talents for the chosen class, this part will be changed once mew support more then druids
				talentsPanel.notifyClassChange();
				
			}
			
			
			
		}
		
	}
	protected void initDataBindings() {
		BeanProperty<CharacterDataSource, Race> characterDataSourceBeanProperty_3 = BeanProperty.create("toon.char_Race");
		BeanProperty<JComboBox, Object> jComboBoxBeanProperty = BeanProperty.create("selectedItem");
		AutoBinding<CharacterDataSource, Race, JComboBox, Object> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, toon, characterDataSourceBeanProperty_3, cmbRace, jComboBoxBeanProperty, "charRace");
		autoBinding_3.bind();
		//
		BeanProperty<CharacterDataSource, Clazz> characterDataSourceBeanProperty_4 = BeanProperty.create("toon.char_Class");
		AutoBinding<CharacterDataSource, Clazz, JComboBox, Object> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, toon, characterDataSourceBeanProperty_4, cmbClass, jComboBoxBeanProperty, "charClass");
		autoBinding_4.bind();
		//
		BeanProperty<CharacterDataSource, Integer> characterDataSourceBeanProperty_5 = BeanProperty.create("toon.char_Level");
		BeanProperty<JSpinner, Object> jSpinnerBeanProperty = BeanProperty.create("value");
		AutoBinding<CharacterDataSource, Integer, JSpinner, Object> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, toon, characterDataSourceBeanProperty_5, spnLevel, jSpinnerBeanProperty, "charLevel");
		autoBinding_5.bind();
	}
}
